package com.xbs.dao.selfdriving;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.xbs.dao.conn.BaseDao;
import com.xbs.dao.conn.ParamSet;
import com.xbs.dao.conn.RowDatas;
import com.xbs.entity.CarManageEntity;

/**
 * 后台车辆管理Dao类
 * @author Administrator
 *
 */
public class CarManageDao extends BaseDao {
	
	//声明一个结果处理对象
	private RowDatas<CarManageEntity> carmanage;
	private RowDatas<CarManageEntity> carmanage1;
	private RowDatas<CarManageEntity> carmanage2;
	
	/**
	 * 实例结果出来对象
	 */
	public CarManageDao(){
		carmanage = new getCarManage();
		carmanage1 = new getCarManage1();
		carmanage2 = new getCarManage2();
	}
	
	/**
	 * 1.查询所有车辆信息
	 * @return
	 */
	public List<CarManageEntity> selAll(){
		String sql = "select * from tb_car left join tb_store on tb_car.store_id = tb_store.store_id left join tb_car_model on tb_car.car_model_id = tb_car_model.car_model_id";
		return queryList(sql, carmanage);
	}
	
	/**
	 * 2.根据车牌号查询车辆信息
	 * @return
	 */
	public List<CarManageEntity> selAll1(String carid){
		String sql = "select * from tb_car left join tb_store on tb_car.store_id = tb_store.store_id left join tb_car_model on tb_car.car_model_id = tb_car_model.car_model_id where car_id = ?";
		return queryList(sql,new ParamSet(carid), carmanage);
	}
	
	/**
	 * 3.根据车型编号查询详细信息
	 * @return
	 */
	public List<CarManageEntity> selAll2(String carmodelid){
		String sql = "select * from tb_car left join tb_store on tb_car.store_id = tb_store.store_id left join tb_car_model on tb_car.car_model_id = tb_car_model.car_model_id where tb_car_model.car_model_id = ?";
		return queryList(sql,new ParamSet(carmodelid), carmanage);
	}
	
	/**
	 * 3.查询车型编号
	 * @return
	 */
	public List<CarManageEntity> select1(){
		String sql = "select car_model_id from tb_car_model";
		return queryList(sql, carmanage1);
	}
	
	/**
	 * 3.查询门店编号
	 * @return
	 */
	public List<CarManageEntity> select2(){
		String sql = "select store_id from tb_store";
		return queryList(sql, carmanage2);
	}
	
	/**
	 * 4.根据车牌号删除车辆信息
	 * @return
	 */
	public int delete(String carid){
		String sql = "delete from tb_car where car_id = ?";
		return updateExecuted(sql,new ParamSet(carid));
	}
	
	/**
	 * 5.添加车辆信息
	 * @return
	 */
	public int insert1(String carid, String modelid, String type, int storeid){
		String sql = "insert into tb_car(car_id, car_model_id, car_type, store_id) values(?, ?, ?, ?)";
		return updateExecuted(sql, new ParamSet(carid, modelid, type, storeid));
	}
	
	/**
	 * 4.根据车牌号修改车辆信息
	 * @return
	 */
	public int update(String carid, String modelid, String type, int storeid, String carid1){
		String sql = "update tb_car set car_id = ?, car_model_id = ?, car_type = ?, store_id = ? where car_id = ? ";
		return updateExecuted(sql,new ParamSet(carid, modelid, type, storeid, carid1));
	}
	
	private class getCarManage implements RowDatas<CarManageEntity>{
		@Override
		public CarManageEntity getRowData(ResultSet rs) throws SQLException {
			CarManageEntity carmanage = new CarManageEntity();
			carmanage.setCar_id(rs.getString("car_id"));
			carmanage.setCar_model_id(rs.getString("car_model_id"));
			carmanage.setCar_type(rs.getString("car_type"));
			carmanage.setStore_id(rs.getInt("store_id"));
			carmanage.setCar_status(rs.getInt("car_status"));
			carmanage.setStore_city(rs.getString("store_city"));
			carmanage.setStore_name(rs.getString("store_name"));
			carmanage.setStore_person(rs.getString("store_person"));
			carmanage.setStore_remark(rs.getString("store_remark"));
			carmanage.setCar_model_name(rs.getString("car_model_name"));
			carmanage.setCar_model_type(rs.getString("car_model_type"));
			carmanage.setCar_model_year(rs.getString("car_model_year"));
			carmanage.setCar_model_seating(rs.getInt("car_model_seating"));
			carmanage.setCar_model_door_number(rs.getInt("car_model_door_number"));
			carmanage.setCar_model_fuel_type(rs.getString("car_model_fuel_type"));
			carmanage.setCar_model_gps(rs.getString("car_model_gps"));
			carmanage.setCar_model_seat(rs.getString("car_model_seat"));
			carmanage.setCar_model_image(rs.getString("car_model_image"));
			carmanage.setCar_model_rent_cash(rs.getDouble("car_model_rent_cash"));
			carmanage.setCar_model_insurance(rs.getDouble("car_model_insurance"));
			carmanage.setCompany_discount(rs.getDouble("company_discount"));
			carmanage.setCar_model_pledge(rs.getDouble("car_model_pledge"));
			return carmanage;
		}
	}
	
	private class getCarManage1 implements RowDatas<CarManageEntity>{
		@Override
		public CarManageEntity getRowData(ResultSet rs) throws SQLException {
			CarManageEntity carmanage = new CarManageEntity();
			carmanage.setCar_model_id(rs.getString("car_model_id"));
			return carmanage;
		}
	}
	
	private class getCarManage2 implements RowDatas<CarManageEntity>{
		@Override
		public CarManageEntity getRowData(ResultSet rs) throws SQLException {
			CarManageEntity carmanage = new CarManageEntity();
			carmanage.setStore_id(rs.getInt("store_id"));
			return carmanage;
		}
	}
}
